package com.example.questionnaires.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnaires.entity.Announcement;
import com.example.questionnaires.mapper.AnnouncementMapper;
import com.example.questionnaires.service.AnnouncementService;
import com.example.questionnaires.common.Result;
import com.example.questionnaires.common.PageResult;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {
    @Override
    public List<Announcement> getLatest() {
        return list(new LambdaQueryWrapper<Announcement>()
                .orderByDesc(Announcement::getCreateTime)
                .last("LIMIT 10"));
    }

    @Override
    public Result<PageResult<Announcement>> list(Integer page, Integer size) {
        Page<Announcement> pageParam = new Page<>(page, size);
        Page<Announcement> pageResult = this.page(pageParam);
        return Result.success(PageResult.build(
            pageResult.getRecords(),
            pageResult.getTotal(),
            pageResult.getSize(),
            pageResult.getCurrent()
        ));
    }
} 